<?php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
	if(isset($_POST['act']) && $_POST['act']=='pagination'){
			$data=cache_get_truyen_chuong_list($_POST['truyen'],$_POST['page']);
			if($data){
				?>
	               <table class="footable table  table-striped" data-filter="#filter" data-filter-text-only="true">
		<thead>
			<tr>
				<th>Danh Sách Chương</th>
				
				
	</tr>
		</thead>
	
		<tbody>
	    	<?php
				foreach($data as $row){
			 ?>
	                  <tr>
	            <td><a href="<?php echo url_truyen_chuong($row['id'],$row['alias'],$_POST['alias']) ?>"> <?php echo $row['ten'] ?></a></td>
	            
			
	          </tr>
	                 <?php }?>
	          
	          </tbody>
	        
	          </table>
	            <?php
			}else{
			?>
	        <h1 class="text-center">Không có dữ liệu</h1>
	        <?php
			}
		
		exit();
	}
	
	if(isset($_POST['act']) && $_POST['act']=='fchuong'){
	
	 	$thutu=intval($_POST['thutu']);	
		$alias=trim($_POST['alias']);
		$truyen=intval($_POST['truyen']);
		$db=new db;
		$db->connect();
		$db->query("select id, alias from truyen_chuong where truyen_id=".$truyen." and thutu=".$thutu);	
		if($db->num_rows()>0){
			$row=$db->fetchOne();
			$url=url_truyen_chuong($row['id'],$row['alias'],$alias);
			echo $url;
		}else{
			echo "no_result";
		}
		exit();
	}
	if($_GET['ajaxLoadSlider'] == 1){
		$page = (int) $_POST['page'];
		
		if(!$page || $page == 0){
			echo json_encode(array('error'=>'Có lỗi xảy ra, vui lòng thử lại.'));
			exit();
		}
		
		$alias = $_POST['alias'];

		
		switch ($alias){
			case 'decu':
				$cache=new cache;
				$_GET['page'] = (int) $_POST['page'];
				$sql ="select id from truyen  where pick = 1 order by id desc ";
				$sql2="select id from truyen where pick = 1";
			
				$pager = new Pagination($sql, DECU_ITEMS, 4, '', 300, "truyen_1");
				$pager->setDebug(false);
				$data['decu'] = $pager->paginate($cache,$sql2);
				
				if(!$data['decu']){
					echo json_encode(array('error'=>'Hết dữ liệu!'));
					exit();
				}

				echo json_encode(array('content'=>render_item_truyen2($data['decu'], 1)));
				break;
				
				case 'truyenmoi':
				$cache=new cache;
				$_GET['page'] = (int) $_POST['page'];
				$sql ="select id from truyen  where duyet = 1 order by id desc ";
				$sql2="select id from truyen where duyet = 1";
			
				$pager = new Pagination($sql,8, 4, '',300, "truyen_1");
				$pager->setDebug(false);
				$data['truyenmoi'] = $pager->paginate($cache,$sql2);
				
				if(!$data['truyenmoi']){
					echo json_encode(array('error'=>'Hết dữ liệu aa!'));
					exit();
				}
			
				echo json_encode(array('content'=>render_item_truyen2($data['truyenmoi'], 1)));
				break;
			case 'video':
				
				$cache=new cache;
				
				$_GET['page'] = (int) $_POST['page'];
				$sql = "select * from index_post where is_video=1 AND pick=1 order by id desc";
				$sql2= "select id from index_post where is_video=1 AND pick=1";
				
				$pager = new Pagination($sql,VIDEO_ITEMS,4,'',300,"post");
				
				$pager->setDebug(false);
				
				$data['video'] = $pager->paginate($cache,$sql2);
				
				if(!$data['video']){
					echo json_encode(array('error'=>'Hết dữ liệu!'));
					exit();
				}
				
				echo json_encode(array('content'=>render_item_video2($data['video'], 1)));
				break;
			
				
		}
		exit();
		
	}
	
	if($_GET['getStoryHistory'] == 1){
		?>
      		<div role="dialog" id="modal_story_history" class="modal">
				<div class="modal-dialog modal-wide" id="modal_report_inner">
					<div class="modal-content">
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					        <h2>Lịch sử đọc truyện <span class="show_history_number"><?php echo '(' . count($_COOKIE['tc']) . ')'; ?></span></h2>
						</div>
						<div class="modal-body" id="modal-content">
		<?php 
		if($_COOKIE['tc']){
			//$_COOKIE['tc'] = stripslashes($_COOKIE['tc']);
			$_COOKIE['tc'] = array_reverse($_COOKIE['tc']);
			$count = 1;
			foreach($_COOKIE['tc'] as $key => $value){


				if(get_magic_quotes_gpc() == 0){
					$value = json_decode($value, true);
				}else{
					$value = json_decode(stripslashes($value), true);
				}

			?>
				<div class="wrap_each_history">
					<span class="badge"><?= $count; ?></span>
					<a class="history_ten_truyen" href="<?= $value['truyen_link']; ?>"><?= $value['truyen'] ?></a> - 
					<a class="history_ten_chuong" href="<?= $value['chuong_link']; ?>"><?= $value['tenchuong']; ?></a>
				</div>
				
			<?php 
			$count++;
			}
		}else{
			echo '<div class="alert alert-warning">Bạn chưa có lịch sử đọc truyện.</div>';
		}
		?>
						</div>
					</div>
				</div>
			</div>
			<script type="text/javascript">
				$('#modal_story_history').modal();
			</script>
		<?php 
		exit();
	}
	
}
?>